package com.example.androidcomplete0102.ch16;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.androidcomplete0102.R;

public class RadioQuestionFrg extends Activity implements ListDialogFragment.NoticeDialogListenter{
	TextView tv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch16_dailogtest);
		tv = (TextView)findViewById(R.id.text);
	}
	
	public void mOnClick(View v){
		ListDialogFragment listFrg = new ListDialogFragment();
		listFrg.show(getFragmentManager(), "dialog");
	}

	@Override
	public void onDialogClick(DialogFragment frg, String res) {
		tv.setText("선택한과일은"+res);
	}
}


class ListDialogFragment extends DialogFragment{
	NoticeDialogListenter mListener;
	int mSelect;
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try{
			mListener = (NoticeDialogListenter)activity;
		}catch(ClassCastException e){
			Log.d("ClassCastException", e.getMessage());
		}
	}
	public interface NoticeDialogListenter{
		public void onDialogClick(DialogFragment frg, String res);
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		return new AlertDialog.Builder(getActivity())
				.setTitle("음식을 선택하세요")
				.setIcon(R.drawable.ic_launcher)
				.setSingleChoiceItems(R.array.fruits, mSelect, new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						mSelect = which;
					}
				})
				
				.setPositiveButton("확인", new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(getActivity(), "which:" +which+ "mSelect:"+mSelect, 0).show();
						String[] fruits = getResources().getStringArray(R.array.fruits);
						mListener.onDialogClick(ListDialogFragment.this, fruits[mSelect]);
					}
				})
				.setNegativeButton("취소", null)
				.create();
		/*return new AlertDialog.Builder(getActivity())
		.setTitle("음식을 선택하세요")
		.setIcon(R.drawable.ic_launcher)
		.setItems(R.array.fruits, new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				String[] fruits = getResources().getStringArray(R.array.fruits);
				mListener.onDialogClick(ListDialogFragment.this, fruits[which]);
			}
		})
		.setNegativeButton("취소", null)
		.create();*/
	}
}

